﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Game.Actions;
using Game.Common;
using Game.Level;
using Game.Objects;
using Game.Map;

namespace Game.Command
{
    public class CommandExecutor : ActionExecutor
    {
        readonly List<ScheduledCommand> _Scheduled = new List<ScheduledCommand>();

        public void IncrementTurnsCompleted()
        {
            var list = new List<ScheduledCommand>(_Scheduled);
            foreach(var command in list)
            {
                command.IncrementTurnsCompleted(this);
            }
        }

        public void Schedule(
            GameTurn turn,
            ScheduledCommand command)
        {
            _Scheduled.Add(command);

            command.Running += (source, args) => _Scheduled.Remove(command);
            command.Canceled += (source, args) => _Scheduled.Remove(command);
        }
        
        public void Execute(
            IGameMapView view,
            GameLevels levels,
            GameTurn turn, 
            GameCommand command)
        {
            base.Execute(view, levels, turn, command);
        }

        public Collection<ScheduledCommand> CommandsForBuilding(GameBuilding building)
        {
            var commands = new Collection<ScheduledCommand>();

            foreach (var command in _Scheduled)
            {
                if (command.GameBuilding == building)
                {
                    commands.Add(command);
                }
            }

            return commands;
        }
        
    }

    public class CommandExecutorEventArgs : EventArgs
    {
        readonly GameCommand _GameCommand;

        public CommandExecutorEventArgs(GameCommand gameCommand)
        {
            _GameCommand = gameCommand;
        }

        public GameCommand GameCommand
        {
            get { return _GameCommand; }
        }

    }

}
